package com.zmn.plat.admin.controller.system.tool.redis;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.response.ResponseUtil;
import com.zmn.common.utils.string.StringUtil;

/**
 * @author nowind
 * @since 2018/07/06 12:11
 */
@Controller
@RequestMapping("/system/tools/redis/")
public class RedisAjaxController extends RedisBaseController {

    // region 查看Redis数据
    @RequestMapping("ajaxDetail.action")
    public void ajaxChartDayLine(HttpServletResponse response, @RequestParam(value = "key", defaultValue = "") String key) {

        if (StringUtil.isBlank(key)) {
            ResponseUtil.outWrite(response, "");
        }

        String value = redisManager.get(key);
        Long ttl = redisManager.ttl(key);
        if (StringUtil.isBlank(value)) {
            if (redisManager.exists(key)) {
                value = "数据为空";
            } else {
                value = "没有找到数据";
            }
        }

        String ttlStr = "";
        if (ttl != null && ttl > 0) {
            ttlStr = String.format("%s（%s）", ttl, DateUtil.getTimespanDesc(ttl));
        }

        ResponseUtil.outWrite(response, String.format("%s~%s", value, ttlStr));
    }
    // endregion
}
